﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

public partial class sort_input : System.Web.UI.Page
{
    string username;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["USERNAME"] == null)
        {
            Response.Redirect("error_log.aspx", true);
        }
        if (Session["IDENTITY"].Equals("employee") == true)
        {
            Response.Redirect("error_auth.aspx", true);
        }
        username = Session["USERNAME"].ToString();
    }
    protected void button1_Click(object sender, EventArgs e)
    {
        //检查textbox1的文本长度若大于5先跳出，小于5继续
        if (textbox1.Text.Length > 5)
        {
            Page.RegisterStartupScript("ss", "<script>alert('请仔细阅读说明，产品分类名称不要超过五个字符！')</script>");
            return;
        }
        else
        {
            //检查textbox1的文本在INFO_SORT中是否存在，若已存在则跳出，若不存在则继续
            SqlConnection sc1 = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
            sc1.Open();
            SqlCommand check = new SqlCommand("select SORT_LEVEL from INFO_SORT where SORT_NAME=@name and ENTER_ID=@eid1", sc1);
            check.Parameters.AddWithValue("@name", textbox1.Text);
            check.Parameters.AddWithValue("@eid1", Session["ENTERID"].ToString());

            if (check.ExecuteScalar() != null)
            {
                sc1.Close();
                Page.RegisterStartupScript("ss", "<script>alert('您已拥有相同名称的产品分类！')</script>");
                return;
            }
            else
            {
                //名称检查完毕，若分类等级为主要产品，检查已有主要产品的数量，若小于4可以继续，等于或大于4跳出
                if (dropdownlist1.Text == "主要产品")
                {
                    SqlCommand get_sum = new SqlCommand();
                    get_sum.Connection = sc1;
                    get_sum.CommandText = "select count(SORT_NAME) from INFO_SORT where SORT_LEVEL=@l and ENTER_ID=@eid2";
                    get_sum.Parameters.AddWithValue("@l", "主要产品");
                    get_sum.Parameters.AddWithValue("@eid2", Session["ENTERID"].ToString());
                    int count = Convert.ToInt32(get_sum.ExecuteScalar().ToString());
                    if (count < 4)
                    {//主要产品写入操作，失败跳出
                       
                        string id =Session["ENTERID"].ToString();
                        //Page.RegisterStartupScript("ss", "<script>alert('"+textbox1.Text+id+"')</script>");
                        SqlCommand insert_sort = new SqlCommand();
                        insert_sort.Connection = sc1;
                        insert_sort.CommandText = "insert into INFO_SORT values (@sname,@eid,@slevel)";
                        insert_sort.Parameters.AddWithValue("@sname", textbox1.Text);
                        insert_sort.Parameters.AddWithValue("@eid", id);
                        insert_sort.Parameters.AddWithValue("@slevel", "主要产品");
                        while (Application["sqllock"] == "lock")
                        {//检测是否已经锁住               
                            System.Threading.Thread.Sleep(2000);
                        }
                        Application["sqllock"] = "lock";//上锁 
                        if (insert_sort.ExecuteNonQuery() == 1)
                        {
                            Application["sqllock"] = "unlock";//解锁 
                            sc1.Close();
                            Page.RegisterStartupScript("ss", "<script>alert('成功添加新的产品分类！')</script>");
                            return;
                        }
                        else
                        {
                            Application["sqllock"] = "unlock";//解锁 
                            sc1.Close();
                            Page.RegisterStartupScript("ss", "<script>alert('数据库异常，请重试！')</script>");
                            return;
                        }

                    }
                    else
                    {
                        sc1.Close();
                        Page.RegisterStartupScript("ss", "<script>alert('您已拥有4个主要产品分类，无法继续添加！')</script>");
                        return;
                    }
                }
                else
                {
                 //若是周边产品，直接写入，失败跳出
                    
                    
                    SqlCommand insert_sort = new SqlCommand();
                    insert_sort.Connection = sc1;
                    insert_sort.CommandText = "insert into INFO_SORT values (@sname,@eid,@slevel)";
                    insert_sort.Parameters.AddWithValue("@sname", textbox1.Text);
                    insert_sort.Parameters.AddWithValue("@eid", Session["ENTERID"].ToString());
                    insert_sort.Parameters.AddWithValue("@slevel", "周边产品");
                    while (Application["sqllock"] == "lock")
                    {//检测是否已经锁住               
                        System.Threading.Thread.Sleep(2000);
                    }
                    Application["sqllock"] = "lock";//上锁 
                    if (insert_sort.ExecuteNonQuery() == 1)
                    {
                        Application["sqllock"] = "unlock";//解锁
                        sc1.Close();
                        Page.RegisterStartupScript("ss", "<script>alert('成功添加新的产品分类！')</script>");
                        return;
                    }
                    else
                    {
                        Application["sqllock"] = "unlock";//解锁 
                        sc1.Close();
                        Page.RegisterStartupScript("ss", "<script>alert('数据库异常，请重试！')</script>");
                        return;
                    }
                }
                
               
            }
        }
    }
}